<?php

namespace app\admin\controller;
use app\admin\model\Node;
use think\Controller;

class Base extends Controller
{
    public static function parse_name($name, $type=0) {
        if ($type) {
           return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
        } else {
           return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
        }
    }
    public function _initialize()
    {

        if(!session('uid')||!session('username')){
            $this->redirect('login/index');
        }
        
        $auth = new \com\Auth();   
        $module     = strtolower(request()->module());
        //$controller = strtolower(request()->controller());
        $controller = request()->controller();
        $controller = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $controller), "_"));
        // $action     = strtolower(request()->action());
        $action     = request()->action();
        $action     =strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $action), "_"));
        $url        = $module."/".$controller."/".$action;

        //跳过检测以及主页权限
        if(session('uid')!=1){
            if(!in_array($url, ['admin/index/index','admin/index/indexpage','admin/upload/upload','admin/index/uploadface'])){
                if(!$auth->check($url,session('uid'))){
                    $this->error('抱歉，您没有操作权限');
                }
            }
        }
        
        $node = new Node();
        $this->assign([
            'username' => session('username'),
            'portrait' => session('portrait'),
            'rolename' => session('rolename'),
            'orgid' => session('orgid'),
            'orgname' => session('orgname'),
            'menu' => $node->getMenu(session('rule'))
        ]);
        $rule = session('rule');
        $this->assign('menulist',$rule );
        $config = cache('db_config_data');

        if(!$config){            
            $config = load_config();                          
            cache('db_config_data',$config);
        }
        config($config); 

        if(config('web_site_close') == 0 && session('uid') !=1 ){
            $this->error('站点已经关闭，请稍后访问~');
        }

        if(config('admin_allow_ip') && session('uid') !=1 ){          
            if(in_array(request()->ip(),explode('#',config('admin_allow_ip')))){
                $this->error('403:禁止访问');
            }
        }

    }
}